package com.one.common.common.login.model;

import com.one.common.config.CPersisData;
import com.one.common.model.bean.UserInfoBean;
import com.one.common.model.http.base.BaseModel;
import com.one.common.model.http.callback.ObserverOnNextListener;
import com.one.common.model.http.callback.ObserverOnResultListener;
import com.one.common.utils.RSAUtils;
import com.one.common.utils.StringUtils;
import com.one.common.view.base.BaseActivity;

import java.util.Map;

/**
 * 登录接口处理
 */
public class LoginModel extends BaseModel<LoginApi> {
    public LoginModel(BaseActivity mActivity) {
        super(LoginApi.class, mActivity);
    }

    /**
     * 注册
     */
    public void register(Map<String, String> params, ObserverOnResultListener<UserInfoBean> listener) {
        handleOnResultObserver(mApiService.register(params), listener);
    }

    /**
     * 发送验证码
     */
    public void sendCode(String mobile, String type, ObserverOnNextListener<String> listener) {
        handleOnNextObserver(mApiService.sendCode(RSAUtils.encryptByPublicKey(mobile), RSAUtils.encryptByPublicKey(type)), listener);
    }

    /**
     * 登录
     */
    public void login(Map<String, String> params, ObserverOnResultListener<UserInfoBean> listener) {
        // 添加极光id
        if(StringUtils.isBlank(CPersisData.getJpushId())){
            CPersisData.setJpushId(mActivity);
        }
        params.put("jpush_id", CPersisData.getJpushId());
        handleOnResultObserver(mApiService.login(params), listener);
    }

    /**
     * 重置密码
     */
    public void resetPwd(Map<String, String> params, ObserverOnResultListener<String> listener) {
        handleOnResultObserver(mApiService.forgetPwd(params), listener);
    }

    /**
     * 微信登陆
     */
    public void wxLogin(String openId, ObserverOnResultListener<UserInfoBean> listener) {
        // 添加极光id
        if(StringUtils.isBlank(CPersisData.getJpushId())){
            CPersisData.setJpushId(mActivity);
        }
//        params.put("jpush_id", CPersisData.getJpushId());
        handleOnResultObserver(mApiService.wxLogin(openId), listener);
    }

    /**
     * 微信登陆--绑定手机号
     */
    public void bindPhone(Map<String, String> params, ObserverOnResultListener<UserInfoBean> listener) {
        handleOnResultObserver(mApiService.bindPhone(params), listener);
    }


}
